document.write("<script type=\"text/javascript\" src=\"../js/checkbox.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"../js/expressions.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"../../seam/resources/lseam.js\"></script>");
document.write("<script type=\"text/javascript\" src=\"../../seam/resources/organizationRemote\"></script>");

// 验证机构表单
function checkOrganizationForm() {
	var organizationForm = document.forms[0];
	if(organizationForm.organizationName.value == "") {
		alert("请输入机构名称！");
		organizationForm.organizationName.focus();
		return false;
	}
	if(organizationForm.phone.value != "" && !EXPRESSION_PHOTO.test(organizationForm.phone.value)) {
		alert("请输入合法的电话！");
		organizationForm.phone.focus();
		return false;
	}
	if(organizationForm.zipcode.value != "" && !EXPRESSION_ZIPCODE.test(organizationForm.zipcode.value)) {
		alert("请输入合法的邮编！");
		organizationForm.zipcode.focus();
		return false;
	}
	return true;
}

// 添加机构
function addOrganization() {
	if(checkOrganizationForm()) {
		var organizationForm = document.forms[0];
		var vo = Lseam.getType("cn.line5.organization.OrganizationVo");
		vo.setParentId(organizationForm.parentId.value);
		vo.setOrganizationName(organizationForm.organizationName.value);
		vo.setOrganizationEnName(organizationForm.organizationEnName.value);
		vo.setOrganizationCode(organizationForm.organizationCode.value);
		vo.setPhone(organizationForm.phone.value);
		vo.setAddress(organizationForm.address.value);
		vo.setZipcode(organizationForm.zipcode.value);
		vo.setManager(organizationForm.manager.value);
		Lseam.getComponent("organizationRemote").addOrganization(vo, function(result) {
    		if(result){
    			alert("添加成功！");
    			window.location.href = "organizationList.seam?organizationId=" + organizationForm.parentId.value;
    		}else{
    			alert("添加失败，请稍候重试！");
    		}
    	});
	}
}

// 编辑机构
function editOrganization() {
	if(getCheckedNum("organizationIds") != 1) {
		alert("必须且只能选择一条编辑的机构！");
		return;
	}
	window.location.href = "organizationEdit.seam?organizationId=" + getCheckboxValue("organizationIds");
}

// 修改机构
function modifyOrganization(tag) {
	if(checkOrganizationForm()) {
		var organizationForm = document.forms[0];
		var vo = Lseam.getType("cn.line5.organization.OrganizationVo");
		vo.setOrganizationId(organizationForm.organizationId.value);
		vo.setOrganizationName(organizationForm.organizationName.value);
		vo.setOrganizationEnName(organizationForm.organizationEnName.value);
		vo.setOrganizationCode(organizationForm.organizationCode.value);
		vo.setPhone(organizationForm.phone.value);
		vo.setAddress(organizationForm.address.value);
		vo.setZipcode(organizationForm.zipcode.value);
		vo.setManager(organizationForm.manager.value);
		Lseam.getComponent("organizationRemote").modifyOrganization(vo, function(result) {
    		if(result) {
    			alert("修改成功！");
    			if(tag == "main")
    				window.location.href = "organizationInfo.seam?organizationId=" + organizationForm.organizationId.value;
    			else
    				window.location.href = "organizationList.seam?organizationId=" + organizationForm.organizationId.value.substring(0, organizationForm.organizationId.value.length - 3);
    		} else
    			alert("修改失败，请稍候重试！");
    	});
	}
}

// 删除机构
function deleteOrganization() {
	if(getCheckedNum("organizationIds") <= 0){
		alert("请选择要删除的机构！");
		return;
	}
	Lseam.getComponent("organizationRemote").checkHaveUser(getCheckboxValues("organizationIds"), function(result) {
		if(result)
			alert("要删除的机构或子机构下有用户，请先将用户移动到其他机构下再进行删除操作！");
		else {
			if(confirm("该操作将删除选择的机构及所属子机构，确定继续吗？")) {
				var organizationForm = document.forms[0];
				organizationForm.method = "post";
				organizationForm.action = "organizationDelete.seam";
				organizationForm.submit();
			}
		}
	});
}

function testIt(obj){
	alert(">>>>"+obj);
}

// 机构向上排序
function upOrganization(organizationId) {
	Lseam.getComponent("organizationRemote").upOrganization(organizationId, function(result) {
		if(result)
			window.location.reload();
		else
			alert("排序操作失败，请稍候重试！");
	});
}

// 机构向下排序
function downOrganization(organizationId) {
	Lseam.getComponent("organizationRemote").downOrganization(organizationId, function(result) {
		if(result)
			window.location.reload();
		else
			alert("排序操作失败，请稍候重试！");
	});
}

// 验证用户表单
function checkUserForm() {
	var userForm = document.forms[0];
	if(userForm.loginName != null && userForm.loginName.value == "") {
		alert("请输入登录名！");
		userForm.loginName.focus();
		return false;
	}
	if(userForm.loginName != null && !EXPRESSION_LOGIN_NAME.test(userForm.loginName.value)) {
		alert("请输入合法的登录名！");
		userForm.loginName.focus();
		return false;
	}
	if(userForm.loginPassword != null && userForm.loginPassword.value == "") {
		alert("请输入密码！");
		userForm.loginPassword.focus();
		return false;
	}
	if(userForm.realName.value == "") {
		alert("请输入真实姓名！");
		userForm.realName.focus();
		return false;
	}
	if(userForm.phone.value != "" && !EXPRESSION_PHOTO.test(userForm.phone.value)) {
		alert("请输入合法的联系电话！");
		userForm.phone.focus();
		return false;
	}
	if(userForm.mobilePhone.value != "" && !EXPRESSION_MOBILE_PHONE.test(userForm.mobilePhone.value)) {
		alert("请输入合法的手机号码！");
		userForm.mobilePhone.focus();
		return false;
	}
	if(userForm.email.value != "" && !EXPRESSION_EMAIL.test(userForm.email.value)) {
		alert("请输入合法的邮箱地址！");
		userForm.email.focus();
		return false;
	}
	return true;
}

// 添加用户
function addUser() {
	if(checkUserForm())
		Lseam.getComponent("organizationRemote").checkLoginName(document.forms[0].loginName.value, function(result) {
			if(!result)
				alert("登录名已被占用，请使用其他登录名！");
			else
				document.forms[0].submit();
		});
}

// 编辑用户
function editUser() {
	if(getCheckedNum("userIds") != 1) {
		alert("必须且只能选择一条编辑的用户！");
		return;
	}
	window.location.href = "userEdit.seam?userId=" + getCheckboxValue("userIds");
}

// 修改用户
function modifyUser(tag) {
	if(checkUserForm())
		document.forms[0].submit();
}

// 删除用户
function deleteUser() {
	if(getCheckedNum("userIds") <= 0){
		alert("请选择要删除的用户！");
		return;
	}
	if(confirm("该操作将删除选择的用户，确定继续吗？")) {
		var userForm = document.forms[0];
		userForm.method = "post";
		userForm.action = "userDelete.seam";
		userForm.submit();
	}
}

// 锁定用户
function lockUser(userId) {
	if(confirm("确定锁定该用户吗？"))
		Lseam.getComponent("organizationRemote").lockUser(userId, function(result) {
		if(result)
			window.location.reload();
		else
			alert("锁定用户失败，请稍候重试！");
		});
}

// 解锁用户
function unlockUser(userId) {
	if(confirm("确定解锁该用户吗？"))
		Lseam.getComponent("organizationRemote").unlockUser(userId, function(result) {
		if(result)
			window.location.reload();
		else
			alert("解锁用户失败，请稍候重试！");
		});
}

// 重置密码
function resetUserPassword(userId) {
	if(confirm("确定重置该用户密码吗？"))
		Lseam.getComponent("organizationRemote").resetUserPassword(userId, function(result) {
		if(result) {
			alert("用户密码重置成功！");
			window.location.reload();
		} else
			alert("重置用户密码失败，请稍候重试！");
		});
}

// 调整用户机构
function changeOrganization() {
	var userForm = document.forms[0];
	if(userForm.newOrganizationId.value == "")
		return;
	if(userForm.organizationId.value == userForm.newOrganizationId.value) {
		userForm.newOrganizationId.value = "";
		return;
	}
	if(getCheckedNum("userIds") <= 0){
		alert("请选择要调整的用户！");
		userForm.newOrganizationId.value = "";
		return;
	}
	if(confirm("确定选择的用户调整到新机构下吗？")) {
		userForm.method = "post";
		userForm.action = "changeOrganization.seam";
		userForm.submit();
	}
}

// 选择用户权限复选框方法
function checkIt(obj) {
	var checked = obj.checked;
	var value = obj.value;
	var el = document.getElementsByTagName("input");
	for (var i = 0; i < el.length; i ++)
		if (el[i].type == "checkbox" && !el[i].disabled)
			if(checked && value.startWith(el[i].value))
				el[i].checked = checked;
			else if(!checked && el[i].value.startWith(value))
				el[i].checked = checked;
}

function setPermission() {
	if(getCheckedNum("permissionModuleIds") <= 0)
		if(!confirm("确定不为该用户设置任何权限吗？"))
			return;
	var permissionForm = document.forms[0];
	permissionForm.method = "post";
	permissionForm.action = "setPermission.seam";
	permissionForm.submit();
}